import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import seasonal_decompose

# 读取CSV文件
file_path = 'Demodata.csv'
df = pd.read_csv(file_path)

# 将Date列转换为日期时间格式
df['Date'] = pd.to_datetime(df['Date'])

# 设置Date列为索引
df.set_index('Date', inplace=True)

# 提取h1到h6列的数据
h_columns = df[['h1', 'h2', 'h3', 'h4', 'h5', 'h6']]

# 分析每一列数据
for column in h_columns.columns:
    h_column = h_columns[column]
    
    # 创建一个新的图形
    plt.figure(figsize=(14, 8))
    
    # 1. 数据可视化
    plt.subplot(2, 1, 1)
    plt.plot(h_column, label=column, color='blue', marker='o')
    plt.title(f'Trend of {column} Column')
    plt.xlabel('Date')
    plt.ylabel('Values')
    plt.legend()
    plt.grid(True)
    
    # 2. 移动平均线
    rolling_mean = h_column.rolling(window=12).mean()
    plt.plot(rolling_mean, label='12-Month Rolling Mean', color='red')
    plt.legend()
    
    # 3. 统计分析
    mean_value = h_column.mean()
    median_value = h_column.median()
    std_dev = h_column.std()
    
    print(f"\n{column} Column Statistics:")
    print(f"Mean: {mean_value}")
    print(f"Median: {median_value}")
    print(f"Standard Deviation: {std_dev}")
    
    # 4. 季节性和周期性分析
    decomposition = seasonal_decompose(h_column, model='additive', period=12)
    trend = decomposition.trend
    seasonal = decomposition.seasonal
    
    plt.subplot(2, 1, 2)
    plt.plot(trend, label='Trend', color='green')
    plt.plot(seasonal, label='Seasonal', color='orange')
    plt.title(f'Trend and Seasonal Components of {column} Column')
    plt.xlabel('Date')
    plt.ylabel('Values')
    plt.legend()
    plt.grid(True)
    
    # 显示图形
    plt.tight_layout()
    plt.show()